import mongoose from 'mongoose'

type comparePasswordFunction = (candidatePassword: string, cb: (err: any, isMatch: any) => void) => void

const userSchema = new mongoose.Schema<UserDocument>({
  email: { type: String, unique: true },
  password: String,
  passwordResetToken: String,
  passwordResetExpires: Date,

  facebook: String,
  twitter: String,
  google: String,
  tokens: Array,

  profile: {
    name: String,
    gender: String,
    location: String,
    website: String,
    picture: String
  }
}, { timestamps: true })

export interface AuthToken {
  accessToken: string
  kind: string
}


export type UserDocument = mongoose.Document & {
  email: string
  password: string
  passwordResetToken: string
  passwordResetExpires: Date

  facebook: string
  twitter: string,
  google: string,
  tokens: AuthToken[]

  profile: {
    name: string
    gender: string
    location: string
    website: string
    picture: string
  }

  comparePassword: comparePasswordFunction
  gravatar: (size: number) => string
}

export const User = mongoose.model<UserDocument>('User', userSchema)